const state = {
  associatedRows: new Map(),
  associatedResults: [],
  fetchParams: {},
  formItems: [],
  formData: {},
  tableColumns: []
}

const mutations = {
  SET_ROW (state, data) {
    const [row, getMapKey] = data ?? []
    state.associatedRows.set(getMapKey(row), row)
  },
  DELETE_ROW (state, data) {
    const [row, getMapKey] = data ?? []
    state.associatedRows.delete(getMapKey(row), row)
  },
  BATCH_SET_ROWS (state, data) {
    const [rows = [], getMapKey] = data ?? []
    rows.forEach(row => {
      state.associatedRows.set(getMapKey(row), row)
    })
  },
  CLEAR_ROWS (state) {
    state.associatedRows = new Map()
  },
  BATCH_DELETE_ROWS (state, data) {
    const [rows = [], getMapKey] = data ?? []
    rows.forEach(row => {
      state.associatedRows.delete(getMapKey(row), row)
    })
  },
  UPDATE_PARAMS (state, {}) {
    state.fetchParams = {}
  },
  SET_FORM_ITEMS (state, items) {
    state.formItems = items
  },
  SET_FORMDATA (state, data) {
    state.formData = data
  },
  UPDATE_FORMDATA (state, { prop, value }) {
    state.formData[prop] = value
  },
  SET_RESULTS (state, results) {
    state.associatedResults = results
  },
  SET_TABLE_COLUMNS (state, columns) {
    state.tableColumns = columns
  }
}

export default {
  namespaced: true,
  state,
  mutations
}
